<?php

class Db_Joins_TopicsJoins
{	
	var $topic_id;
	var $page_id;
	var $page_size = 30;
	var $skip_pagination;
	
	function __construct() {
		$this->db = Zend_Registry::get("db");
	}
	
	function GetTopicList()
	{
		$result = array();
		
		//count total for pagination
		$data = $this->db->select();
		$data->from("topics", array("count(topic_id) as ct"));
		$ct = $this->db->fetchRow($data);

		//dump all in condition
		$data = $this->db->select();
		$data->from("topics", "*");
		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		$data->order("topic_id DESC");
		$rows = $this->db->fetchAll($data);
		
		$result = array(
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $rows
		);
		
		return $result;
	}
	
	function GetTopicInfo()
	{
		$result = array();
		
		if($this->topic_id)
		{
			$data = $this->db->select();
			$data->from("topics", "*");
			$data->where("topic_id = ?", $this->topic_id);
			$result = $this->db->fetchRow($data);
		}
		
		return $result;
	}
}